﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace website.template_faa.admin
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    public class getDanWeiList : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            //点击datagrid的分页按钮，自动向后台发送2个参数,rows和page，代表每页记录数和页索引  
            int row = int.Parse(context.Request["rows"].ToString());
            int page = int.Parse(context.Request["page"].ToString());
            System.Data.SqlClient.SqlDataReader reader = GetContent("zhiFaDW", row, page);
            int total = GetRecordCount("zhiFaDW");
            string text = dbcaac.JsonHelper.SqlDataReader2JsonForEasyUIDataGrid(total, reader);
            context.Response.Write(text);
            context.Response.End();
        }

        /// <summary>
        /// 访问数据库，获得指定表的记录条数
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private int GetRecordCount(string table)
        {
            int total = -1;
            string sql = "select  COUNT(*) as number from " + table;
            System.Data.SqlClient.SqlDataReader reader = jsm.SqlHelper.SqlServerHelper.ExecuteReader(sql);
            if (reader.Read())
            {
                total = int.Parse(reader["number"].ToString());
            }
            return total;
        }

        /// <summary>
        /// 访问数据库，获得分页记录
        /// </summary>
        /// <param name="pagesize">每页记录数</param>
        /// <param name="pageindex">页索引</param>
        /// <returns>DataSet</returns>
        private System.Data.SqlClient.SqlDataReader GetContent(string table, int pagesize, int pageindex)
        {
            System.Data.SqlClient.SqlParameter[] paras = new System.Data.SqlClient.SqlParameter[8];
            paras[0] = new System.Data.SqlClient.SqlParameter("@pageSize", pagesize);
            paras[1] = new System.Data.SqlClient.SqlParameter("@currentPage", pageindex);
            paras[2] = new System.Data.SqlClient.SqlParameter("@columns", "zfdwId, zfdwName, zfdwAbbr, zfdwAddr, zfdwLinkman, zfdwTel");
            paras[3] = new System.Data.SqlClient.SqlParameter("@tableName", table);
            paras[4] = new System.Data.SqlClient.SqlParameter("@condition", "");
            paras[5] = new System.Data.SqlClient.SqlParameter("@ascColumn", "zfdwId");
            paras[6] = new System.Data.SqlClient.SqlParameter("@bitOrderType", 0);
            paras[7] = new System.Data.SqlClient.SqlParameter("@pkColumn", "zfdwId");

            return jsm.SqlHelper.SqlServerHelper.ExecuteReaderProc("dbcaac_GetPageOfRecords", paras);
        }  

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
